//获取应用实例
const app = getApp()
import { ajax, ROOT_URL } from "../../utils/util.js";
Page({
  data: {
    url:ROOT_URL,
    banner: [],
    indexArr:[],
    isInfo:false,
    userName:'',
    userImg:'',
    userNum:'',
    title:[]
  }, 
  onLoad: function () {
    let _this = this;
    var openid = wx.getStorageSync('openId').openid;
    //获取用户信息
    wx.getUserInfo({
      success: function (res) {
        _this.setData({
          isInfo: false
        })
        ajax('/index/User/user_info', {
          data: {
            nickname: res.userInfo.nickName,
            headImg: res.userInfo.avatarUrl,
            openid: openid
          }
        }, res => {
          if (res.status == 200) {
            _this.setData({
              userName: res.data.user_nickName,
              userImg: res.data.user_hearImage,
              userNum: res.data.user_keyNum
            })
          }
        })
      },
      fail: function () {
        //获取用户信息失败后。请跳转授权页面
        _this.setData({
          isInfo: true
        })
      }
    })
    //banner图
    ajax('/index/banner/banner_list', {
    }, res => {
      if (res.status == 200) {
        this.setData({
          banner: res.data
        })
      }
    })
    
    ajax('/index/Index/title', {
    }, res => {
      // console.log(res)
      if (res.status == 200) {   
        this.setData({
          title: res.data
        })
      }
    })
    this.indexData();
  },

  indexData(){
    //首页列表
    ajax('/index/brand/top_ten', {
    }, res => {
      wx.stopPullDownRefresh();
      if (res.status == 200) {
        this.setData({
          indexArr: res.data
        })
      }
    },err => {
      console.log(err)
      wx.stopPullDownRefresh();
    })
    
  },
  //点击授权
  onGetserInfo(e){
    let userInfo = e.detail.userInfo;
    var openid = wx.getStorageSync('openId').openid;

    // console.log(openid)
    ajax('/index/User/user_info', {
      data:{
        nickname: userInfo.nickName,
        headImg: userInfo.avatarUrl,
        openid: openid
      }
    }, res => {
      // console.log(res+'授权')
      if (res.status == 200) {
        // console.log(JSON.stringify(res)+'uid')
        var key = wx.getStorageSync('openId') || {};
        key.userid = res.data.user_id;
        wx.setStorageSync("openId", key);
        this.setData({
          userName: res.data.user_nickName,
          userImg: res.data.user_hearImage,
          userNum: res.data.user_keyNum,
          isInfo: false
        })
      }
    },err=>{
      consoe.log(err)
    })
  },
  //跳转投票页
  tapVote() {
    let name = this.data.userName;
    let img = this.data.userImg;
    let num = this.data.userNum;
    wx.navigateTo({
      url: '/pages/vote/vote?name=' + name + '&img=' + img + '&num=' + num,
    })
  },
  tabTest(){
    wx.navigateTo({
      url: '/pages/testhome/testhome',
    })
  },
  tabDateils(){
    let name = this.data.userName;
    let img = this.data.userImg;
    let num = this.data.userNum;
    wx.navigateTo({
      url: '/pages/details/details?name=' + name + '&img=' + img + '&num=' + num,
    })
  },
  homeClick(e){
    let listId = e.currentTarget.id;
    wx.navigateTo({
      url: '/pages/showHome/showHome?id=' + listId,
    })
  },
   /* 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _this = this;
    var openid = wx.getStorageSync('openId').openid;
    //获取用户信息
    wx.getUserInfo({
      success: function (res) {
        // console.log(res+'index')
        // console.log('获取成功'+'index')
        _this.setData({
          isInfo: false
        })
        ajax('/index/User/user_info', {
          data: {
            nickname: res.userInfo.nickName,
            headImg: res.userInfo.avatarUrl,
            openid: openid
          }
        }, res => {
          // console.log(res+'用户信息成功')
          if (res.status == 200) {
            var key = wx.getStorageSync('openId') || {};
            key.userid = res.data.user_id;
            wx.setStorageSync("openId", key);
            _this.setData({
              userName: res.data.user_nickName,
              userImg: res.data.user_hearImage,
              userNum: res.data.user_keyNum,
              isInfo: false
            })
          }
        })
      },
      fail: function () {
        console.log('获取失败' + 'index')
        //获取用户信息失败后。请跳转授权页面
        _this.setData({
          isInfo: true
        })
      }
    })
    this.indexData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh(){
    this.indexData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})
